//
//  KLMBaseButton.swift
//  KLM_GN
//
//  Created by Nic on 2025/2/26.
//

import UIKit

enum ContentPosition {
    /// 图片在左，文字在右
    case imageLeading(spacing: CGFloat)
    /// 图片在右，文字在左
    case imageTrailing(spacing: CGFloat)
    /// 图片在上，文字在下
    case imageTop(spacing: CGFloat)
    /// 图片在下，文字在上
    case imageBottom(spacing: CGFloat)
}

class KLMBaseButton: UIButton {
    /// 内容位置
    private var contentPosition: ContentPosition = .imageLeading(spacing: 10.0)
    ///  按钮大小是否自适应内容
    private var sizeEqualToContent: Bool = false
    
    /// 初始化方法
    init(position: ContentPosition = .imageLeading(spacing: 6),
         image: String? = nil,
         title: String? = nil,
         sizeEqualToContent: Bool = false
    ) {
        super.init(frame: .zero)
        setupContent(position: position, image: image, title: title, sizeEqualToContent: sizeEqualToContent)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    func setupContent(position: ContentPosition, image: String? = nil, title: String?, sizeEqualToContent: Bool = false) {
        if let image = image, !image.isEmpty {
            setImage(UIImage(named: image)?.withRenderingMode(.alwaysOriginal), for: .normal)
        }
        if let title = title {
            setTitle(title, for: .normal)
        }
        contentPosition = position
        self.sizeEqualToContent = sizeEqualToContent
        
        switch contentPosition {
        case .imageLeading(_):
            titleLabel?.textAlignment = .left
        case .imageTrailing(_):
            titleLabel?.textAlignment = .right
        case .imageTop(_), .imageBottom(_):
            titleLabel?.textAlignment = .center
        }
        setNeedsLayout()
        invalidateIntrinsicContentSize()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
//        adjustImageAndTitlePosition()
        adjustSize()
    }
    
    override var intrinsicContentSize: CGSize {
        guard sizeEqualToContent else { return super.intrinsicContentSize }
        
        let imageSize = imageView?.intrinsicContentSize ?? .zero
        let titleSize = titleLabel?.intrinsicContentSize ?? .zero
        let spacing: CGFloat
        
        switch contentPosition {
        case .imageLeading(let s), .imageTrailing(let s):
            spacing = s
            return CGSize(
                width: imageSize.width + titleSize.width + spacing,
                height: max(imageSize.height, titleSize.height)
            )
            
        case .imageTop(let s), .imageBottom(let s):
            spacing = s
            return CGSize(
                width: max(imageSize.width, titleSize.width),
                height: imageSize.height + titleSize.height + spacing
            )
        }
    }
    
    private func adjustSize() {
        imageView?.sizeToFit()
        titleLabel?.sizeToFit()
        
        guard let imageSize = imageView?.frame.size,
              let titleSize = titleLabel?.frame.size else {return }
        if imageSize == .zero && titleSize == .zero {
            return
        }
        if imageSize == .zero {
            titleLabel?.frame = bounds
        } else if titleSize == .zero {
            imageView?.frame = bounds
        } else {
            adjustImageAndTitlePosition(imageSize: imageSize, titleSize: titleSize)
        }
    }
    
    // MARK: - Private Methods
    private func adjustImageAndTitlePosition(imageSize: CGSize, titleSize: CGSize) {
        
        switch contentPosition {
        case .imageLeading(let spacing):
            imageEdgeInsets = UIEdgeInsets(top: 0, left: -spacing/2, bottom: 0, right: spacing/2)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: spacing/2, bottom: 0, right: -spacing/2)
            titleLabel?.y = (size.height - titleSize.height) * 0.5
            imageView?.y = (size.height - imageSize.height) * 0.5
        case .imageTrailing(let spacing):
            titleEdgeInsets = UIEdgeInsets(
                top: 0,
                left: -(imageSize.width + spacing/2),
                bottom: 0,
                right: imageSize.width + spacing/2
            )
            imageEdgeInsets = UIEdgeInsets(
                top: 0,
                left: titleSize.width + spacing/2,
                bottom: 0,
                right: -(titleSize.width + spacing/2)
            )
            titleLabel?.y = (size.height - titleSize.height) * 0.5
            imageView?.y = (size.height - imageSize.height) * 0.5
        case .imageTop(let spacing):
            imageEdgeInsets = UIEdgeInsets(
                top: -(titleSize.height + spacing/2),
                left: 0,
                bottom: 0,
                right: -titleSize.width
            )
            titleEdgeInsets = UIEdgeInsets(
                top: imageSize.height + spacing/2,
                left: -imageSize.width,
                bottom: 0,
                right: 0
            )
            titleLabel?.x = (size.width - titleSize.width) * 0.5
            imageView?.x = (size.width - imageSize.width) * 0.5
        case .imageBottom(let spacing):
            imageEdgeInsets = UIEdgeInsets(
                top: titleSize.height + spacing/2,
                left: 0,
                bottom: 0,
                right: -titleSize.width
            )
            titleEdgeInsets = UIEdgeInsets(
                top: -(imageSize.height + spacing/2),
                left: -imageSize.width,
                bottom: 0,
                right: 0
            )
            titleLabel?.x = (size.width - titleSize.width) * 0.5
            imageView?.x = (size.width - imageSize.width) * 0.5
        }
    }
}
